<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class Publisher extends CI_Model
{
	private $publisher_info_table_name			= 'publisher_info_v4';
	private $host_channels_table_name			= 'host_channels';
	private $afc_info_table_name			= 'afc_info_v4';
	private $afs_info_table_name			= 'afs_info';
	
	function __construct()
	{
		parent::__construct();
		$ci =& get_instance();
		$ci->load->database();
	}
	function insert_new_host_channel($response){
		$channel = array();
		$channel['channelName'] = $response['name'];
		$channel['channelID'] = $response['id'];
		$channel['status'] = 'On';
		$this->db->insert($this->host_channels_table_name, $channel);
		
		if($this->db->affected_rows() > 0 ) return true;
		else return false;
	}
	function get_host_channels(){
		$query_result = $this->db->get_where($this->host_channels_table_name, array('status' => 'On'));
		if($query_result->num_rows() > 0){
			$channels = array(''=>'-');
			foreach($query_result->result_array() as $key=>$val){
				$channels[$val['channelName']] = $val['channelName'];
			}
			return $channels;
		}
		else{ //return empty array
			return $channels;
		}
	}
	
	function insert_info_deprecated($info){
		//echo "<pre>";print_r($info);echo "</pre>";
		
		$query_result = $this->db->get_where($this->publisher_info_table_name, array('loginEmail' => $info['loginEmail']));
		
		if($query_result->num_rows() > 0){ //update
			$this->db->where('loginEmail', $info['loginEmail']);
			$info['modifiedTime'] = date('Y-m-d H:i:s', time());
			
			$this->db->update($this->publisher_info_table_name, $info);	
		}
		else{ // insert
			
			$this->db->insert($this->publisher_info_table_name, $info);
		}
		
		if($this->db->affected_rows() > 0 ) return true;
		else return false;
	}
	
	function insert_info($info){
		
		$query_result = $this->db->get_where($this->publisher_info_table_name, array('accountId' => $info['accountId']));
		
		if($query_result->num_rows() > 0){ //update
			$this->db->where('accountId', $info['accountId']);
			$info['modifiedTime'] = date('Y-m-d H:i:s', time());
			
			$this->db->update($this->publisher_info_table_name, $info);	
		}
		else{ // insert
			
			$this->db->insert($this->publisher_info_table_name, $info);
		}
		
		if($this->db->affected_rows() > 0 ) return true;
		else return false;
	}
	
	function insert_afc_info($info){
		$query_result = $this->db->get_where($this->afc_info_table_name, array('googleID' => $info['googleID'],'AFCName'=>$info['AFCName']));
		
		if($query_result->num_rows() > 0){ //same name ad recorded
			return false;
		}
		else{ // insert
			$this->db->insert($this->afc_info_table_name, $info);
		}
		
		if($this->db->affected_rows() > 0 ) return true;
		else return false;
	}
	
	function insert_afs_info($info){
		$query_result = $this->db->get_where($this->afs_info_table_name, array('googleID' => $info['googleID'],'AFSName'=>$info['AFSName']));
		
		if($query_result->num_rows() > 0){ //same name ad recorded
			return false;
		}
		else{ // insert
			$this->db->insert($this->afs_info_table_name, $info);
		}
		
		if($this->db->affected_rows() > 0 ) return true;
		else return false;
	}
	
	function test_afc_info($info){
		$query_result = $this->db->get_where($this->afc_info_table_name, array('accountID' => $info['accountID'],'name'=>$info['name']));
		if($query_result->num_rows() > 0){ //same name ad recorded
			return false;
		}
		else{ // insert
			return true;
		}
	}
	
	function test_afc_info_deprecated($info){
		$query_result = $this->db->get_where($this->afc_info_table_name, array('googleID' => $info['googleID'],'AFCName'=>$info['AFCName']));
		if($query_result->num_rows() > 0){ //same name ad recorded
			return false;
		}
		else{ // insert
			return true;
		}
	}
	
	function test_afs_info($info){
		$query_result = $this->db->get_where($this->afs_info_table_name, array('googleID' => $info['googleID'],'AFSName'=>$info['AFSName']));
		if($query_result->num_rows() > 0){ //same name ad recorded
			return false;
		}
		else{ // insert
			return true;
		}
	}
	
	function get_publishers_overview(){
		$this->db->select();
		$query_result = $this->db->get($this->publisher_info_table_name);
		return $query_result->result_array();
	}
	
	function get_publisher_by_account_id($account_id){
		$query_result = $this->db->get_where($this->publisher_info_table_name, array('AFC' => 'ca-'.$account_id));
		return $query_result->result_array();
	}
	
	function get_publisher_by_afs_id($googleID){
		$query_result = $this->db->get_where($this->publisher_info_table_name, array('googleID_SearchAds' => $googleID));
		return $query_result->result_array();
	}
	
	function get_publisher_by_sfc_id($googleID){
		$query_result = $this->db->get_where($this->publisher_info_table_name, array('googleID_SearchAds' => $googleID));
		return $query_result->result_array();
	}
	
	function get_afc_overview($googleID){
		$select_header = $this->config->item('show_header_afc', 'publisher');
		$header_string = '';
		foreach($select_header as $key=>$val){
			$header_string .= $key.',';
		}
		
		$header_string = substr($header_string, 0, -1);
		
		$this->db->select($header_string);
		$query_result = $this->db->get_where($this->afc_info_table_name,array('googleID' => $googleID));
		return $query_result->result_array();
	}
	
	function get_afs_overview($googleID){
		$select_header = $this->config->item('show_header_afs', 'publisher');
		$header_string = '';
		foreach($select_header as $key=>$val){
			$header_string .= $key.',';
		}
		
		$header_string = substr($header_string, 0, -1);
		
		$this->db->select($header_string);
		$query_result = $this->db->get_where($this->afs_info_table_name,array('googleID' => $googleID));
		return $query_result->result_array();
	}
	
	function get_afc_code($afc){
		$this->db->select('code');
		$query_result = $this->db->get_where($this->afc_info_table_name, array('googleID' => $afc['googleID'],'AFCName' => $afc['AFCName']));
		//echo "<pre>";print_r($query_result);echo "</pre>";
		
		return $query_result->result_array();
	}
	
	function get_afs_code($afs){
		$this->db->select('code');
		$query_result = $this->db->get_where($this->afs_info_table_name, array('googleID' => $afs['googleID'],'AFSName' => $afs['AFSName']));
		//echo "<pre>";print_r($query_result);echo "</pre>";
		
		return $query_result->result_array();
	}
	
}

/* End of file users.php */
/* Location: ./application/models/auth/users.php */